package team.cdlzz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import team.cdlzz.service.UploadService;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author zw
 * @date 2022-06-26.
 */
@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @PostMapping("image")
    public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return ResponseEntity.ok(uploadService.uploadImage(file));
    }

    @PostMapping("userImg")
    public String upload(@RequestParam("file") MultipartFile file,@RequestParam("uid") Integer uid) throws Exception {
        return uploadService.uploadImage(file,uid);
    }

   @PostMapping("upCourse")
    public String upCourse(@RequestParam("file") MultipartFile file) throws Exception {
         String s = uploadService.uploadCourse(file);
        return s;
    }

    @PostMapping("excel")
    public ResponseEntity<String> uploadExcel(@RequestParam("file-excel") MultipartFile fileExcel) throws Exception {
        return ResponseEntity.ok(uploadService.uploadImage(fileExcel));
    }
    @PostMapping("courseFile")
    public String  uploadCourseFile(@RequestParam("file") MultipartFile fileExcel,
                                                   @RequestParam("cid") Integer cid,
                                                   @RequestParam("uid")Integer uid) throws Exception {
        String s = uploadService.uploadCourseFile(fileExcel, cid, uid);
        System.out.println(s+"==== uid:" +uid +"=== cid:" +cid);
        return s;
    }

    /**
     * 实施大纲下载
     *
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/download")
    public String downloadDoc(HttpServletResponse response,
                              @RequestParam(value = "filename", defaultValue = "cjtouxiang.jpg") String filename) throws UnsupportedEncodingException {
        //下载后的文件名
        return uploadService.downLoad(response,filename);
    }

}
